package com.arron.algorithm.leetcodetop100.单调栈;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class 下一个更大元素II {




    public int[] nextGreaterElements(int[] nums) {

        int len = nums.length;

        int[] res = new int[len];

        LinkedList<Integer> stack = new LinkedList<>();
        for (int i = 2*len-1; i >= 0; i--) {

            while (!stack.isEmpty() && stack.getLast() <= nums[i%len]){
                stack.pollLast();
            }

            res[i%len] = stack.isEmpty()?-1:stack.getLast();
            stack.addLast(nums[i%len]);
        }


       return res;
    }

    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>();


        int[] arr ={1,2,3,4};
        int n = arr.length;


        //循环访问数组
        int index =0;
//        while (true){
//            if (index > 2*n) break;
//            System.out.println((index-1)&(n-1));
////            System.out.println(a[index%n]);
//            index++;
//        }

        while (true) {
            // 在环形数组中转圈 ,仅适用于数组长度为2的幂次方，如2，4，8，16，32....等
            System.out.println(arr[index & (arr.length - 1)]);
            index++;
        }



    }

}
